﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SchoolCardSystem
{
    public partial class Login : System.Web.UI.Page
    {
        //定义数据库信息
        MySqlConnection connection = new MySqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["connection"].ConnectionString.ToString());
        //定义数据集
        
        //DataSet mydata = new DataSet();   
        //获取后端验证码
        String checkcode;
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void LoginBtn_Click(object sender, EventArgs e)
        {
            //打开数据库
            connection.Open();
            checkcode = Session["CheckCode"].ToString();
            //从表单中获取用户信息
            String name = userName.Text;
            String pass = userPass.Text;
            //String yzm = userIdent.Text;

            //检查信息是否为空
            if (userName.Text.Trim() == "")
            {
                userError.Style["display"] = "inline";
                userError.Text = "学号不能为空";
                userName.Focus();
                return;
            }
            else if (userPass.Text.Trim() == "")
            {
                userError.Text = "密码不能为空";
                userPass.Focus();
                return;
            }
            //查询姓名表单
            //调用数据库用户信息
            string sqlstr = @"select userId,userPass from user where userId=@userId";
            //Command 查询语句
            MySqlCommand cmd = new MySqlCommand(sqlstr, connection);
            //设置@userId的类型
            cmd.Parameters.Add(new MySqlParameter("@userId",MySqlDbType.VarChar,11));
            cmd.Parameters["@userId"].Value = userName.Text;
            
            //在线模式
            MySqlDataReader dr = cmd.ExecuteReader();
            
            //离线模式
            ////Adapter 存储数据
            //MySqlDataAdapter Adapter = new MySqlDataAdapter();
            //Adapter.SelectCommand = cmd;
            ////DataSet 数据分表
            //Adapter.Fill(mydata,"userId");
            //DataColumn col1 = mydata.Tables["userId"].Columns[0];
            
            //查看卡号是否存在
            if(!dr.Read()){
                userError.Style["display"] = "inline";
                userError.Text = "卡号不存在";
                userName.Text = "";
                userName.Focus();
            }
            //验证码不正确
            else if(userIdent.Text.Trim().ToUpper()!= checkcode)
            {
                userError.Style["display"] = "inline";
                userError.Text = "验证码错误!";
                codeImg.ImageUrl = "ValidataCode.aspx";
                userIdent.Text = "";
                userPass.Focus();
            }
            //登陆成功 跳转
            else if (dr["userPass"].ToString().Trim() == userPass.Text.Trim())
            {
                String user = userName.Text.Trim();
                Session["name"] = userName.Text.Trim();
                Response.Redirect("index.aspx");
            }
            //密码错误
            else
            {
                userError.Style["display"] = "inline";
                userError.Text = "密码错误!";
                userPass.Text = "";
                userPass.Focus();
            }
            //关闭数据库
            connection.Close();
        }

        protected void codeImg_Click(object sender, ImageClickEventArgs e)
        {
            Session.Clear();
            codeImg.ImageUrl = "ValidataCode.aspx";
        }
    }
}